package com.hliushi.knife4j.web.user;

import com.hliushi.knife4j.entity.User;
import com.itheima.pinda.user.annotation.LoginUser;
import com.itheima.pinda.user.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author llhuang10
 * @date 2021/8/22 19:40
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @GetMapping("/getUsers")
    @ApiOperation(value = "查询所有用户", notes = "查询所有用户信息")
    public List<User> getAllUsers() {
        User user = new User();
        user.setId(100);
        user.setName("itcast");
        user.setAge(20);
        user.setAddress("bj");
        List<User> list = new ArrayList<>();
        list.add(user);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增用户", notes = "新增用户信息")
    public String save(@RequestBody User user) {
        return "OK";
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改用户", notes = "修改用户信息")
    public String update(@RequestBody User user) {
        return "OK";
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户", notes = "删除用户信息")
    public String delete(int id) {
        return "OK";
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码",
                    required = true, type = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",
                    required = true, type = "Integer"),
    })
    @ApiOperation(value = "分页查询用户信息")
    @GetMapping(value = "page/{pageNum}/{pageSize}")
    public String findByPage(@PathVariable Integer pageNum,
                             @PathVariable Integer pageSize) {
        return "OK";
    }

    @GetMapping("/get-currentUser")
    public SysUser getCurrentUser(@LoginUser SysUser user) {//注入当前登录用户
        log.info("================= {}", user);
        return user;
    }


    /**
     * 测试 异常
     */
    @GetMapping("/get")
    public String get() {
        // 2021-08-26 11:15:28.581  WARN 17944 --- [nio-8001-exec-1] c.i.p.c.h.DefaultGlobalExceptionHandler  : Exception:
        //
        // java.lang.ArithmeticException: / by zero
        // 	at com.hliushi.knife4j.web.user.UserController.get(UserController.java:81)
        // {
        //   "code": -1,
        //   "data": "",
        //   "msg": "系统繁忙~请稍后再试~",
        //   "path": "/user/get",
        //   "extra": null,
        //   "timestamp": 1629947728583,
        //   "isSuccess": false,
        //   "isError": true
        // }
        int i = 1 / 0;
        return "success";
    }
}
